// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// N [author] rhyme_lph
// W [email]  rhymelph@gmail.com
// C [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/credit_note_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/utils/reg_exp_utils.dart';
import 'package:finance_app/src/widgets/button/add_button.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/credit_note_number_radio.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/credit_note/credit_note_number_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "creditNoteNumberPage")
class CreditNoteNumberPage extends StatefulWidget {
  @override
  _CreditNoteNumberPageState createState() => _CreditNoteNumberPageState();
}

class _CreditNoteNumberPageState extends State<CreditNoteNumberPage>
    with PagePresenter<CreditNoteNumberPage, CreditNoteNumberProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.credit_note_number);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            NormalFormWrapper(
              child: AddButton(
                title: S.current.add_credit_note,
                onTap: provider.onNavAddCreditNote,
              ),
            ),
            AppSpaces.grayH05,
            Expanded(
              child: listBuilder<String>(
                selector: (_, __) => __.searchController.text,
                builder: (BuildContext context, String value, Widget child) {
                  List<Widget> childList = buildSearchWidget(value);
                  return childList.isEmpty
                      ? super.buildEmptyWidget
                      : ListView(
                          children: childList,
                        );
                },
              ),
            ),
          ],
        ),
      );

  List<Widget> buildSearchWidget(String value) {
    List<Widget> slivers;
    if (provider.searchController.text.isNotEmpty) {
      slivers = provider.data
          .toList()
          .where(_onSearchInputGroup)
          .map((e) => buildSliverItem(e))
          .toList();
    } else {
      slivers = provider.data.toList().map(buildSliverItem).toList();
    }
    return slivers;
  }

  Widget buildSliverItem(CreditNoteEntity data) {
    return listBuilder<Tuple2<CreditNoteEntity, bool>>(
      selector: (_, __) => Tuple2(data, data.id == __.currentSelect?.id),
      builder: (BuildContext context, Tuple2<CreditNoteEntity, bool> value,
              Widget child) =>
          PressedOpacityButton(
        onPressed: () => provider.onSelect(value.item1),
        child: CreditNoteNumberRadio(
          title: value.item1.contact.name,
          creditNoteNo: value.item1.number,
          issueDate: DateTimeUtils.formatDateTimeString(value.item1.issuedAt),
        ),
      ),
    );
  }

  Widget buildSliverHeader({String title}) {
    final String _title = title.isEmpty ? "*" : title;
    return ListHeaderItem(
      title: _title,
    );
  }

  bool _onSearchInputGroup(CreditNoteEntity element) {
    String searchInput = provider.searchController.text;
    return RegExpUtils.getSearchRegExp(searchInput)
        .hasMatch(element.contact.name);
  }
}
